package com.lure.base.statemachine.impl;

import com.lure.base.statemachine.Transition;
import com.lure.base.statemachine.exception.StateMachineException;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yanwei li create  2024-05-29 01:11
 */
public class EventTransitions <S,E,C>{

    private Map<E, List<Transition<S,E,C>>> eventTransitions = new HashMap<>();


    public void put(E event,Transition<S, E, C> transition ){
        List<Transition<S, E, C>> transitions = eventTransitions.get(event);
        if (transitions == null) {
            transitions = new ArrayList<>();
            transitions.add(transition);
            eventTransitions.put(event,transitions);
        }else {
            List<Transition<S, E, C>> newTransitions = new ArrayList<>(transitions);
            if (newTransitions.contains(transition)) {
                throw new StateMachineException(transition + " already Exist, you can not add another one");
            }
            newTransitions.add(transition);
            eventTransitions.put(event,newTransitions);
        }

    }

    public List<Transition<S,E,C>> getEventTransition(E event){
        final List<Transition<S, E, C>> transitions = eventTransitions.get(event);
        return transitions;
    }
}
